package cn.chengpengper.seckill.activity.interfaces.controller;

import cn.chengpengper.seckill.activity.application.service.SeckillActivityService;
import cn.chengpengper.seckill.activity.domain.model.entity.SeckillActivity;
import cn.chengpengper.seckill.common.code.HttpCode;
import cn.chengpengper.seckill.common.model.dto.SeckillActivityDTO;
import cn.chengpengper.seckill.common.response.ResponseMessage;
import cn.chengpengper.seckill.common.response.ResponseMessageBuilder;
import cn.chengpengper.seckill.common.utils.date.JodaDateTimeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 秒杀活动接口
 *
 * @author chengpeng.hu
 */
@RestController
@RequestMapping("/activity")
@RequiredArgsConstructor
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*", originPatterns = "*")
public class SeckillActivityController {

    private final SeckillActivityService seckillActivityService;

    /**
     * 保存秒杀活动
     *
     * @param seckillActivityDTO 秒杀活动
     */
    @PostMapping("/v1/saveSeckillActivity")
    public ResponseMessage<String> saveSeckillActivityDTO(@RequestBody SeckillActivityDTO seckillActivityDTO) {
        seckillActivityService.saveSeckillActivity(seckillActivityDTO);
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode());
    }

    /**
     * 根据活动状态活动列表
     *
     * @param status 活动状态
     * @return 活动列表
     */
    @GetMapping("/v1/getSeckillActivityList")
    public ResponseMessage<List<SeckillActivity>> getSeckillActivityList(
            @RequestParam(value = "status") Integer status
    ) {
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillActivityService.getSeckillActivityList(status));
    }

    /**
     * 根据时间和状态获取秒杀活动列表
     *
     * @param currentTime 当前时间
     * @param status      秒杀状态
     * @return 活动列表
     */
    @GetMapping("/v1/getSeckillActivityListBetweenStartTimeAndEndTime")
    public ResponseMessage<List<SeckillActivity>> getSeckillActivityListBetweenStartTimeAndEndTime(
            @RequestParam(value = "currentTime", required = false) String currentTime,
            @RequestParam(value = "status", required = false) Integer status
    ) {
        List<SeckillActivity> seckillActivityList = seckillActivityService.getSeckillActivityBetweenStartTimeAndEndTime(JodaDateTimeUtils.parseStringToDate(currentTime, JodaDateTimeUtils.DATE_TIME_FORMAT), status);
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillActivityList);
    }

    /**
     * 获取秒杀活动列表
     *
     * @param status  活动状态
     * @param version 版本号
     * @return 活动列表
     */
    @GetMapping("/v1/seckillActivityList")
    public ResponseMessage<List<SeckillActivityDTO>> getSeckillActivityList(@RequestParam(value = "status") Integer status,
                                                                            @RequestParam(value = "version") Long version) {
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillActivityService.getSeckillActivityList(status, version));
    }

    /**
     * 获取活动详情
     *
     * @param id      活动ID
     * @param version 版本
     * @return 活动详情
     */
    @GetMapping("/v1/seckillActivity")
    public ResponseMessage<SeckillActivityDTO> getSeckillActivityById(@RequestParam(value = "id") Long id,
                                                                      @RequestParam(value = "version") Long version) {
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillActivityService.getSeckillActivity(id, version));
    }

    /**
     * 根据活动ID获取活动信息
     *
     * @param id 活动ID
     * @return 活动信息
     */
    @GetMapping("/v1/getSeckillActivityById")
    public ResponseMessage<SeckillActivity> getSeckillActivityById(@RequestParam(value = "id") Long id) {
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillActivityService.getSeckillActivityById(id));
    }

    /**
     * 更新描述活动状态
     *
     * @param status 活动状态
     * @param id     活动ID
     */
    @PutMapping("/v1/updateStatus")
    public ResponseMessage<String> updateStatus(
            @RequestParam(value = "status") Integer status,
            @RequestParam(value = "id") Long id
    ) {
        seckillActivityService.updateStatus(status, id);
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode());
    }
}
